<template>
  <div class="page">
    <!-- <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="名称">
          <el-input v-model="searchForm.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="searchForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form> -->
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="add">新增</el-button>
      <div style="width:20px"></div>
      <!-- <el-button type="danger" @click="del">删除</el-button> -->
    </div>
    <el-table :data="tableData" border style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }">
      <!-- <el-table-column type="selection"></el-table-column> -->
      <!-- <el-table-column prop="lessionid" label="ID"></el-table-column> -->
      <el-table-column prop="lessionname" label="课程名称"></el-table-column>
      <el-table-column prop="lessioninfo" label="课程简介"></el-table-column>
      <!-- <el-table-column prop="img" label="封面"></el-table-column> -->
      <el-table-column prop="img" label="封面" width="150">
        <template slot-scope="scop">
          <el-image fit="fil" style="width: 80px; height: 50px" :src="scop.row.img.indexOf('http')>-1?scop.row.img:$Url + '/upload/' + scop.row.img"
            :preview-src-list="[scop.row.img.indexOf('http')>-1?scop.row.img:$Url + '/upload/' + scop.row.img]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="isdel" label="状态" width="100">
        <template slot-scope="scop">
          <el-tag type="success" v-if="scop.row.isdel == 0">暂停</el-tag>
          <el-tag type="warning" v-if="scop.row.isdel == 1">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" width="120">
        <template slot-scope="scop">
          <el-button type="primary" icon="el-icon-edit" circle @click="edit(scop.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div> -->
    <el-dialog title="新增/修改课程" center :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="addOrEditForm" ref="RefAddOrEditForm" :rules="rule" label-width="100px" class="edit-form"
        label-suffix="：">
        <el-form-item label="课程名称" prop="lessionname">
          <el-input type="text" placeholder="请输入课程名称" v-model="addOrEditForm.lessionname"></el-input>
        </el-form-item>
        <el-form-item label="课程简介" prop="price">
          <el-input type="text" placeholder="请输入课程简介" v-model="addOrEditForm.lessioninfo"></el-input>
        </el-form-item>
        <el-form-item label="封面" prop="img">
          <upload v-model="addOrEditForm.img" />
        </el-form-item>
        <el-form-item label="状态" prop="isdel">
          <el-radio-group v-model="addOrEditForm.isdel">
            <el-radio :label="0">暂停</el-radio>
            <el-radio :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { findLessions, addLessions, updateLessions, delLessions } from "@/api/user";
import upload from "@/components/uploadFile";
export default {
  components: {
    upload
  },
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      searchForm: {
        name: "",
        phone: ""
      },
      dialogFormVisible: false,
      addOrEditForm: {
        lessionid: '',
        lessionname: '',
        lessioninfo: '',
        img: '',
        isdel: '',
      },
      rule: {
        lessionname: [
          { required: true, message: '请设置课程名称', trigger: 'change' }
        ],
        lessioninfo: [
          { required: true, message: '请设置课程简介', trigger: 'change' }
        ],
        // img: [
        //   { required: true, message: '请上传课程封面', trigger: 'change' }
        // ],
        isdel: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }
    };
  },
  computed: {
    queryData() {
      return {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await findLessions.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    edit(row) {
      this.dialogFormVisible = true
      const { lessionid, lessionname, lessioninfo, img, isdel } = row
      this.addOrEditForm = {
        lessionid,
        lessionname,
        lessioninfo,
        img,
        isdel
      }
    },
    add() {
      this.dialogFormVisible = true
      this.addOrEditForm = {
        lessionid: '',
        lessionname: '',
        lessioninfo: '',
        img: '',
        isdel: ''
      }
    },
    save() {
      this.$refs.RefAddOrEditForm.validate(async (valid) => {
        if (valid) {
          if (this.addOrEditForm.lessionid === '') {
            const res = await addLessions.post(this.addOrEditForm);
            if (res.resultCode === "000000") {
              this.$message.success(res.resultInfo);
              this.dialogFormVisible = false
              this.getData()
            } else {
              this.$message.error(res.resultInfo);
            }
          } else {
            const res = await updateLessions.post(this.addOrEditForm);
            if (res.resultCode === "000000") {
              this.$message.success(res.resultInfo);
              this.dialogFormVisible = false
              this.getData()
            } else {
              this.$message.error(res.resultInfo);
            }
          }

        } else {
          return false;
        }
      });
    },
    del(row) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delImg(row.lessionid)
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async delImg(lessionid) {
      const res = await delLessions.post({ lessionid });
      if (res.resultCode === "000000") {
        this.$message.success("删除成功！");
        this.getData();
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="less" scoped></style>
